<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8">
		<title></title>
		<link type="text/css" rel="stylesheet" href="{SKIN_PATH}statics/css/haidao.css?v={HD_VERSION}" />
		<link type="text/css" rel="stylesheet" href="{SKIN_PATH}statics/css/public.css?v={HD_VERSION}" />
		<script type="text/javascript" src="{SKIN_PATH}statics/js/jquery-1.7.2.min.js?v={HD_VERSION}"></script>
		<script type="text/javascript" src="{SKIN_PATH}statics/js/haidao.web.general.js?v={HD_VERSION}"></script>
		<script type="text/javascript" src="{SKIN_PATH}statics/js/common.js?v={HD_VERSION}"></script>
		<script type="text/javascript" src="{__ROOT__}statics/js/dialog/dialog-plus-min.js?v={HD_VERSION}"></script>
		<link rel="stylesheet" type="text/css" href="{__ROOT__}statics/js/dialog/ui-dialog.css?v={HD_VERSION}" />
		<script type="text/javascript" src="{__ROOT__}statics/js/haidao.validate.js?v={HD_VERSION}"></script>
	</head>
	<body>
		<form name="district">
		<!-- 新增地址 -->
		<div class="add-address" style="width: 100%;">
			<ul class="double-line text-left clearfix">
				<li class="list">
					<span class="label">收货地区：</span>
					<div class="content">
						<input type="hidden" name="district_id" value="0">
						<select id="district">
							<option>请选择地区</option>
						</select>
						<span id="check-area" class="text-mix">&nbsp;</span>
					</div>
				</li>
				<li class="list">
					<span class="label">详细地址：</span>
					<div class="content">
						<textarea class="wide textarea" name="address" /></textarea>
						<span id="check-address" class="text-mix"></span>
					</div>
				</li>
				<li class="list">
					<span class="label">收货人：</span>
					<div class="content">
						<input class="input" name="name" type="text" />
						<span id="check-name" class="text-mix"></span>
					</div>
				</li>
				<li class="list">
					<span class="label">手机号：</span>
					<div class="content">
						<input class="input" name="mobile" type="text" />
						<span id="check-mobile" class="text-mix"></span>
					</div>
				</li>
				<li class="list">
					<span class="label">邮政编码：</span>
					<div class="content">
						<input class="input" name="zipcode" type="text" />
						<span id="check-zipcode" class="text-mix"></span>
						<div class="layout">
							<label><input name="default" type="checkbox" class="fl margin-right margin-top" />设为默认地址</label>
						</div>
					</div>
				</li>
			</ul>
		</div>
		<div class="padding border-top bg-gray-white text-right">
			<input class="button bg-sub" id="hold" type="button" value="保存" />
			<input class="margin-left button bg-gray" id="cancel" type="button" value="取消" />
		</div>
		</form>
		<script type="text/javascript" src="{SKIN_PATH}statics/js/haidao.linkage.js?v={HD_VERSION}"></script>
		<script type="text/javascript">
		$("#district").linkageSel({
			url: '{url('ajax_district')}',
			callback: function(vals,tar){
				$("input[name=district_id]").val(vals[vals.length-1]);
			}
		});
		
		$(function(){
			try {
				var dialog = top.dialog.get(window);
			} catch (e) {
				return;
			}
			dialog.title("新增收货地址");
			dialog.reset();
			
			$("input[name=name]").blur(function(){
				checkName();
			});
			$("input[name=mobile]").blur(function(){
				checkMobile();
			});
			$("[name=address]").blur(function(){
				checkAddress();
			});
			$("input[name=zipcode]").blur(function(){
				checkZipcode();
			});
			
			$("#hold").click(function(){
				if ($("#hold").prop('disabled') == false) {
					submithandle();
				}
				$("#hold").attr('disabled',true);
			});
			
			$("#cancel").click(function(){
				dialog.remove();
			});

			function checkName(){
				if(!$("input[name=name]").val()){
					$("#check-name").text("请您填写收货人姓名！");
					$("#hold").attr('disabled',true);
					return false;
				}
				$("#check-name").text("");
				$("#hold").attr('disabled',false);
				return true;
			}
			
			function checkMobile(){
				var str = $("input[name=mobile]").val();
				if(!str){
					$("#check-mobile").text("请您填写收货人手机号码！");
					$("#hold").attr('disabled',true);
					return false;
				}
				if(!str.match(/^1[3|4|5|7|8]\d{9}$/)){
					$("#check-mobile").text("手机号码格式不正确！");
					$("#hold").attr('disabled',true);
					return false;
				}
				$("#check-mobile").text("");
				$("#hold").attr('disabled',false);
				return true;
			}
			
			function checkAddress(){
				var str = $("[name=address]").val();
				if(!str){
					$("#check-address").text("请您填写收货人详细地址！");
					$("#hold").attr('disabled',true);
					return false;
				}
				if(str.length<6){
					$("#check-address").text("收货地址至少六个字符！");
					$("#hold").attr('disabled',true);
					return false;
				}
				$("#check-address").text("");
				$("#hold").attr('disabled',false);
				return true;
			}
			
			function checkZipcode(){
				var str = $("input[name=zipcode]").val();
				if(str && !str.match(/^[0-9][0-9]{5}$/)){
					$("#check-zipcode").text('邮编格式不正确');
					$("#hold").attr('disabled',true);
					return false;
				}
				$("#check-zipcode").text('');
				$("#hold").attr('disabled',false);
				return true;
			}
			
			function submithandle(){
				
				if(!checkName()){
					$("input[name=name]").focus();
					return false;
				}
				if(!checkMobile()){
					$("input[name=mobile]").focus();
					return false;
				}
				if(!checkAddress()){
					$("input[name=address]").focus();
					return false;
				}
				if(!checkZipcode()){
					$("input[name=address]").focus();
					return false;
				}
				var flog = true;
				$('select').each(function(){
					if($(this).val()==''){
						flog = false;
					}
				});
				if(!flog){
					$("#check-area").text("请您选择完整的地区信息！")
					return false;
				}
				$.post("{url('member/address/add')}",$("form").serialize(), function(ret) {
					if(ret.status == 0) {
						$.tips({
							icon:'error',
							content:ret.message,
							callback:function() {
								return false;
							}
						});
					} else {
						$.tips({
							icon:'success',
							content:ret.message,
							callback:function() {
								dialog.close("ok");
							}
						});					
					}
					return false;
				},'json');
			}
		});
		</script>
	</body>
</html>